package com.waterfairy.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by water_fairy on 2016/12/2.
 */

public class DateUtils {
    private static String TAG = "timeUtils >>";

    public static String trans(long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        return simpleDateFormat.format(date);
    }

    /**
     * 系统图片保存样式   系统没有sss
     *
     * @param time
     * @return
     */
    public static String trans3(long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmSSsss");
        return simpleDateFormat.format(date);
    }

    public static String trans2(long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    public static long trans2(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.parse(time).getTime();
    }

    public static String trans(String timeStr) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));//时区
//        calendar.getTime().getTime();
//        Fri Jan 17 11:14:45 CST 2016
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss CST yyyy");
        String content = "";
        try {
            Date parse = dateFormat.parse("Fri Jan 17 11:14:45 CST 2014");//解析
            return trans(parse.getTime());
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return "";
    }

    public static String getDateStr(String date, String formatSrc, String formatTarget) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatSrc);
        try {
            Date parse = formatter.parse(date);
            return getDateStr(parse, formatTarget);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param currentDate 当前时间
     * @param transDate   需要转化的时间(默认转化为 前天,昨天,今天,明天,后天)
     * @param detail      是否更详细(现在,5分钟前,半小时前,n个小时前)
     * @return
     */
    public String transWhichDate(Date currentDate, Date transDate, boolean detail) {
        int currentYear = currentDate.getYear();
        int currentMonth = currentDate.getMonth();
        int currentDay = currentDate.getDay();
        int currentHours = currentDate.getHours();
        int currentMinutes = currentDate.getMinutes();
        return "";
    }

    /**
     * @param currentTime   当前系统时间
     * @param currentFormat 当前系统时间格式
     * @param targetTime    需要转换的时间
     * @param targetFormat  需要转换的时间格式
     * @return
     */
    public static String dateFormat(String currentTime, String currentFormat,
                                    String targetTime, String targetFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(currentFormat);
        try {
            Date currentDate = simpleDateFormat.parse(currentTime);
            simpleDateFormat.applyPattern(targetFormat);
            Date targetDate = simpleDateFormat.parse(targetTime);
            long dTime = 0;
            if ((dTime = currentDate.getTime() - targetDate.getTime()) >= 0) {
                if (dTime < 60000) {
                    //小于1分钟
                    return "刚刚";
                } else if (dTime < 3600000) {
                    //小于1小时
                    return dTime / (60000) + "分钟前";
                } else if (dTime < 3600000 * 24) {
                    //小于1天
                    return dTime / 3600000 + "小时前";
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return targetTime;
    }


    public static String getDateStr(Date date, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static String handlerData(String data) {
        String[] strings = data.split("-");
        if (strings.length > 2) {
            return strings[0] + "年" + strings[1] + "月" + strings[2] + "日";
        } else {
            return data;
        }
    }
}
